package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class TestSoulApiImpl implements TestSoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    // 查询问卷列表
    @Override
    public List<Survey> findSurvey() {
        // Query query = Query.query(Criteria.where("surveyId").is());
        // List<Survey> survey = mongoTemplate.find(query, Survey.class);
        List<Survey> surveys = mongoTemplate.findAll(Survey.class);     // 查询全部的问卷列表
        return surveys;
    }

    @Override
    public Survey findSurvey(Long surveyId) {
        Query query = Query.query(Criteria.where("surveyId").is(surveyId));
        return mongoTemplate.findOne(query, Survey.class);
    }

    // 查询单条问卷信息
    @Override
    public Long findQuestion(Long questionId) {
        Query query = Query.query(Criteria.where("questionId").is(questionId));
        return mongoTemplate.findOne(query, Questions.class).getSurveyId();
    }

    // 查询问题列表
    @Override
    public List<Questions> findQuestions(Long surveyId) {
        Query query = Query.query(Criteria.where("surveyId").in(surveyId));
        List<Questions> questions = mongoTemplate.find(query, Questions.class);
        return questions;
    }

    // 查询答案列表
    @Override
    public List<Answer> findAnswer(Long questionId) {
        Query query = Query.query(Criteria.where("questionId").in(questionId));
        List<Answer> answer = mongoTemplate.find(query, Answer.class);
        return answer;
    }

    // 查询试题id和选项id查询详细记录
    @Override
    public Answer findAnswer(Long qusetionId, Long optionId) {
        Query query = Query.query(Criteria.where("questionId").is(qusetionId).and("optionId").is(optionId));
        return mongoTemplate.findOne(query, Answer.class);
    }

    // 根据试题id和选项id查询答案详情
    @Override
    public Report findReport(Integer type) {
        Query query = Query.query(Criteria.where("reportType").is(type));
        Report report = mongoTemplate.findOne(query, Report.class);
        return report;
    }

    // 根据报告id 查询报告
    @Override
    public Report findReport(Long reportId) {
        Query query = Query.query(Criteria.where("reportId").is(reportId));
        return mongoTemplate.findOne(query, Report.class);
    }

    // 保存单条问卷
    @Override
    public void saveSurvey(Survey survey) {
        mongoTemplate.save(survey);
    }

    // 根据用户id和类型查询做题记录
    @Override
    public Boolean findWorkById(Long userId, Integer type) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("surveyType").is(type));
        return mongoTemplate.exists(query, Work.class);
    }

    // 判断是否做题
    @Override
    public Boolean findWorkById(Long reportId, Long userId) {
        Query query = Query.query(Criteria.where("reportId").is(reportId).and("userId").is(userId));
        return mongoTemplate.exists(query, Work.class);
    }

    // 保存做题记录
    @Override
    public void saveWork(Work userWork) {
        mongoTemplate.save(userWork);
    }

    @Override
    public Work findWork(Long userId, Integer type) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("surveyType").is(type));
        return mongoTemplate.findOne(query, Work.class);
    }


    @Override
    public Long findSurveyByType(String name) {
        Query query = Query.query(Criteria.where("name").is(name));
        return mongoTemplate.findOne(query, Survey.class).getSurveyId();
    }


}
